package name.matthewgreet.example7.action; import org.apache.struts2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.interceptor.validation.SkipValidation; import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator; import name.matthewgreet.example7.form.ArithmeticForm; /** * Tests Struts 2 automatic type conversion for integers. */ //@InterceptorRef(value="defaultStack", params={"conversionError.excludeMethods", "submit"}) @InterceptorRef("defaultStack") @Results({ @Result(name=ActionSupport.ERROR, location="/pages/ArithmeticSeparateForm.jsp"), @Result(name=ActionSupport.INPUT, location="/pages/ArithmeticSeparateForm.jsp"), @Result(name=ActionSupport.SUCCESS, location="/pages/ArithmeticSeparateForm.jsp") }) public class ArithmeticSeparateFormAction extends ActionSupport { private static final long serialVersionUID = -6084392315736424463L; private ArithmeticForm form; private String resultText; @Action("/ArithmeticSeparateForm") @Override @SkipValidation public String execute() throws Exception { try { int result; if (form == null) { resultText = ""; } else if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() * form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } @Action("/ArithmeticSeparateForm_submit") public String submit() throws Exception { try { int result; if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() + form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } @VisitorFieldValidator(appendPrefix = true) public ArithmeticForm getForm() { if (form == null) { form = new ArithmeticForm(); } return form; } public String getResultText() { return resultText; } }